package com.datang.petstore.service.impl;

import com.datang.petstore.entity.*;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogLikeRecordMapper;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogLikeRecordService;
import com.datang.petstore.service.MicroblogService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-01 14:02
 * @notify 微博点赞
 * @version 1.0
 */
@Service
public class MicroblogLikeRecordServiceImpl implements MicroblogLikeRecordService {

    @Autowired
    private MicroblogService microblogService;

    @Autowired
    private MicroblogLikeRecordMapper microblogLikeRecordMapper;

    /*
     * @author 顶风少年
     * @date 2019/11/1 7:26
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param likeRecord
     * @result com.datang.petstore.utils.Result
     * @notify 点赞
     * version 1.0
     */
    @Transactional
    public void addLike(String userId, Integer microblogId) {

        //判断微博是否不存在或者被删除
        Microblog microblog = microblogService.getMicroblog(microblogId);
        if (microblog == null || microblog.getDel().equals("1")) {
            throw new AcceptResultCodeException(ResultCode.error505);
        }

        //判断用户是否对该微博点赞过
        List<MicroblogLikeRecord> likeRecords = getLikeRecord(userId, microblogId);
        if (likeRecords.size() != 0) {
            throw new AcceptResultCodeException(ResultCode.error506);
        }


        MicroblogLikeRecord likeRecord = new MicroblogLikeRecord();
        likeRecord.setMicroblogId(microblogId);
        likeRecord.setMicroblogUserId(microblog.getUserId());
        likeRecord.setUserId(userId);
        likeRecord.setDate(new Date());
        //添加记录
        microblogLikeRecordMapper.insertSelective(likeRecord);

        //更改微博点赞记录数
        microblog.setLikeNumber(microblog.getLikeNumber() + 1);
        microblogService.updateMicroblog(microblog);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/1 15:39
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @param microblogId
     * @result java.util.List<com.datang.petstore.entity.LikeRecord>
     * @notify 根据指定微博id和用户id查询点赞记录
     * version 1.0
     */
    public List<MicroblogLikeRecord> getLikeRecord(String userId, Integer microblogId) {
        MicroblogLikeRecordExample likeRecordExample = new MicroblogLikeRecordExample();
        MicroblogLikeRecordExample.Criteria criteria = likeRecordExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andMicroblogIdEqualTo(microblogId);
        List<MicroblogLikeRecord> likeRecords = microblogLikeRecordMapper.selectByExample(likeRecordExample);
        return likeRecords;
    }
}
